// 管理员模版
// 引入获取假数据
import manager from "../mock/manager-bian";
const adminModule = {
  nameSpace: true,
  state: {
    adminList: [],
  },
  // getters: {
  //     renderList(state) {
  //         return state.adminList.slice(0, 8);
  //     },
  // },
  mutations: {
    getData(state, payload) {
      state.adminList = payload;
      console.log(state.adminList);
    },
    // getters: {
    //     renderList(state) {
    //         return state.adminList.slice(0, 8);
    //     },
    // },
  },
  actions: {
    async getDataAsync({ commit }) {
      const res = await manager.getData();
      commit("getData", res.managerData.data);
      // console.log(res.managerData.data);
    },
    async delDataAsync({ commit }, payload) {
      const res = await manager.delData(payload.id);
      commit("getData", res.managerData.data);
      // console.log(res.managerData.data);
    },
    async addDataAsync({ commit }, payload) {
      const res = await manager.addData(payload);
      console.log(payload);
      commit("getData", res.managerData.data);
      // console.log(111);
      // console.log(res.managerData.data);
    },
    async changeDataAsync({ commit }, payload) {
      const res = await manager.changeData(payload);
      // console.log(payload);
      // console.log(res);
      commit("getData", res.managerData.data);
      // console.log(res.managerData.data);
    },
    async searchDataAsync({ commit }, payload) {
      const res = await manager.getData(payload);
      console.log(res);
      let newList = [];
      // let flag = true;
      if (payload.value === "") {
        newList = res.managerData.data;
      } else {
        res.managerData.data.forEach((item) => {
          // 遍历每个数组项即对象的属性值
          Object.values(item).forEach((i) => {
            if (payload.value === i) {
              // flag = false;
              newList = [].concat(item);
            }
          });
        });
      }
      commit("getData", newList);
      // console.log(res.managerData.data);
    },
  },
};

export default adminModule;
